package org.example.judge_contest.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.judge_common.annotation.AuthAnnotation;
import org.example.judge_common.entity.PageResult;
import org.example.judge_common.entity.RequestParams;
import org.example.judge_common.entity.ResponseResult;
import org.example.judge_contest.request.RequestContestAnnounceParams;
import org.example.judge_contest.service.ContestAnnouncementService;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.ContestAnnouncement;
import org.example.judge_pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/announcement")
public class ContestAnnouncementController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Qualifier("ContestAnnouncementServiceImpl")
    @Autowired
    private ContestAnnouncementService announcementService;


    @AuthAnnotation({Auth.CLIENT})
    @PostMapping("/search")
    public ResponseResult list(RequestContestAnnounceParams params,HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");

        PageResult<ContestAnnouncement> res = announcementService.search(params);
        if(loginUser!=null&&res.getDataList().size()>0){ //此时刷新消息，即不在提醒最新的
//            redisTemplate.opsForValue().set(loginUser.getId()+"_announce",res.getDataList().get(0).getId());
        }
        return ResponseResult.success().setData(res);
    }

    /**
     * 获取最新的比赛公告
     * @param contestId
     * @return
     */
    @AuthAnnotation({Auth.CLIENT})
    @PostMapping("/new")
    public ResponseResult getNew(String contestId,HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");

        //获取新的通知
        String o = (String) redisTemplate.opsForValue().get(contestId + "_announce");
        if(o==null)return ResponseResult.success();
        //获取上次读到的通知ID
        Object id = redisTemplate.opsForValue().get(loginUser.getId() + "_announce");
        ContestAnnouncement announcement = JSON.parseObject(o, ContestAnnouncement.class);
//        System.out.println(announcement);
//        System.out.println(id);
        if(announcement.getId().equals(id)){ //两次相同，不需要
            return ResponseResult.success();
            //不同表示为新消息，并置为老消息
        }else redisTemplate.opsForValue().set(loginUser.getId()+"_announce",announcement.getId());
        return ResponseResult.success().setData(announcement);
    }
    @AuthAnnotation({Auth.ADMINISTRATOR})
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ContestAnnouncement announcement, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
        announcement.setUserId(loginUser.getId());
        announcementService.save(announcement);
        redisTemplate.opsForValue().set(announcement.getContestId()+"_announce",JSON.toJSONString(announcement),5, TimeUnit.HOURS);
        return ResponseResult.success();
    }
    @AuthAnnotation({Auth.ADMINISTRATOR})
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ContestAnnouncement announcement, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
        announcement.setUserId(loginUser.getId());
        announcementService.updateById(announcement);
//        redisTemplate.opsForValue().set(announcement.getContestId()+"_announce",JSON.toJSONString(announcement),5, TimeUnit.HOURS);
//        redisTemplate.delete(announcement.getContestId()+"_announce");
        return ResponseResult.success();
    }

    @AuthAnnotation({Auth.ADMINISTRATOR})
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") String announcementId){
        ContestAnnouncement byId = announcementService.getById(announcementId);
        if(byId==null)return ResponseResult.NotData();
        announcementService.removeById(announcementId);
        redisTemplate.delete(byId.getContestId()+"_announce");
        return ResponseResult.success();
    }
}
